package com.example.jpa1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AddPoem {
    //https://so.gushiwen.cn/gushi/tangshi.aspx
    public static String getHTML(String urlstr) throws IOException {
        URL url = new URL(urlstr);
        HttpURLConnection con =
                (HttpURLConnection)
                        url.openConnection();
        BufferedReader br =
                new BufferedReader(
                        new InputStreamReader(
                                con.getInputStream(),
                                "UTF-8"));
        StringBuffer sb = new StringBuffer();
        String s;
        while((s=br.readLine())!=null){
            sb.append(s);
        }
        // System.out.println(sb.toString());
        br.close();
        con.disconnect();
        return sb.toString();
    }


    public static void saveData(PoemWjmRepository poemWjmRepository) throws IOException, InterruptedException {

        String html =
                getHTML("https://so.gushiwen.cn/gushi/tangshi.aspx");

        String content = html;
        String pattern = "<a href=\"(/shiwenv_.*?\\.aspx)\" target=\"_blank\">.*?</a>";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(content);
        List<String> list = new ArrayList<>();
        while(m.find()){
            list.add(m.group(1));
        }

        int min = 1;
        int max = 10;
        for(int i=0;i<32;i++){
            AddPoemer addPoemer =
                    new AddPoemer
                            (list,
                                    min+i*10,max+i*10,poemWjmRepository);
            addPoemer.start();
        }

    }
}

